﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using System.Data.OleDb;

using shopsoft.models;
using shopsoft.dbutility;

namespace shopsoft.business.SystemConfig
{
    /// <summary>
    /// 用户权限认证
    /// </summary>
    public class SCUserRoleAuthority
    {
        MsSqlHelper msSqlHelper = new MsSqlHelper();

        #region  CheckUserPassword Methods

        /// <summary>
        /// Check User And Password
        /// </summary>
        /// <param name="users">员工</param>
        /// <returns></returns>
        public bool CheckUserPassword(Users users)
        {
            try
            {
                var parameters = new List<OleDbParameter>()
                {
                    new OleDbParameter("UserNo", OleDbType.VarChar,50)
                };
                parameters[0].Value = users.UserNO;

                string strSql = @"select Operator_No, Operator_Password, Operator_Name  
                                  from t_Operator_Info 
                                  where Operator_No = @UserNo";
                DataSet ds;

                msSqlHelper.RunProcedureOrSql(strSql, parameters.ToArray(), "t_Operator_Info", 2, out ds);

                if (ds.Tables["t_Operator_Info"].Rows.Count > 0)
                {
                    if (ds.Tables["t_Operator_Info"].Rows[0]["Operator_Password"].ToString() == users.PassWord)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                
                throw;
            }
        }
        #endregion

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="users">员工</param>
        /// <param name="operationLog">操作日志</param>
        /// <returns></returns>
        /// <remarks>
        /// 操作代码 (1.成功返回0_员工ID 2.失败返回错误代码（1_0001已停用，1_0010密码错误，1_0011账号不存在，0010操作内部错误）)
        /// </remarks>
        public Users UsersLogin(Users users)//, OperationLog operationLog)
        {
            try
            {
                var parameters = new List<OleDbParameter>()
                {
                    new OleDbParameter("@UserNO", OleDbType.VarChar,50)
                    //new OleDbParameter("@Password", users.PassWord)
                    //new OleDbParameter("@IPAddress", users.IPAddress)
                };
                parameters[0].Value = users.UserNO;

                Users user = new Users();
                
                //用户登录验证
                string strSql = @"select Operator_ID as UserID, Operator_No as UserNO, Operator_Name as UserName, Company_ID, Store_ID   
                    from t_Operator_Info where Operator_No=@UserNO and Delete_No='0'";

                OleDbDataReader reader;

                msSqlHelper.RunProcedureOrSql(strSql, parameters.ToArray(), 2, out reader);

                if (reader.Read())
                {
                    if (reader.FieldCount > 1)
                    {
                        user.ID = Convert.ToInt32(reader["UserID"]);
                        user.Name = reader["UserName"].ToString();
                        user.UserNO = reader["UserNO"].ToString();

                        user.Company = new Company {
                            ID = int.Parse(reader["Company_ID"].ToString())
                        };// reader["UserNO"].ToString();

                        user.Store = new Store {
                            ID = int.Parse(reader["Store_ID"].ToString())
                        };

                        //user.IPAddress = reader["IPAddress"].ToString();
                        //user.RoleInfo = new RoleInfo() { ID = int.Parse(reader["RoleID"].ToString()) };
                    }
                    else
                    {
                        string errorCode = reader["ErrorCode"].ToString();
                        //log.Info(errorCode);
                        throw new ConnectionException(errorCode);
                    }
                }

                reader.Close();

                return user;
            }
            catch (OleDbException oleDbException)
            {
                //log.Fatal(sqlException.Message);
                throw new ConnectionException("数据库连接失败", oleDbException);
            }
        }
    }
}
